# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
# class Solution:
#     def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
#         pointers=[]
#         while head:
#             pointers.append(head)
#             head=head.next
#         if len(pointers)==1:
#             return None
#         if n==1:
#             pointers[-n-1].next=None
#             return pointers[0]
#         if n==len(pointers):
#             return pointers[1]
#         pointers[-n-1].next=pointers[-n+1]
#         return pointers[0]

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        count=0
        dummy=ListNode(0)
        dummy.next=head
        fast=head
        head=dummy
        while count<n-1:
            fast=fast.next
            count+=1
        while fast.next:
            head=head.next
            fast=fast.next
        head.next=head.next.next
        return dummy.next